package com.example.controller;

import com.example.domain.Device;
import com.example.domain.Result;
import com.example.service.DeviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/devices")
public class DeviceController {

    @Autowired
    private DeviceService deviceService;

    @GetMapping
    public Result getAll(){
        return new Result(true, deviceService.getAll());
    }

    @GetMapping("{deviceId}")
    public Result getById(@PathVariable Integer deviceId){
        return new Result(true,deviceService.getById(deviceId));
    }

    @PostMapping
    public Result add(@RequestBody Device device) {
        boolean code = deviceService.add(device);
        return new Result(code);
    }

    @PutMapping
    public Result update(@RequestBody Device device){
        boolean code = deviceService.update(device);
        return new Result(code);
    }

    @DeleteMapping("{deviceId}")
    public Result delete(@PathVariable Integer deviceId){
        boolean code = deviceService.delete(deviceId);
        return new Result(code);
    }

}